package com.xijia.wuyouwallet.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xijia.wuyouwallet.MyApp;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseActivity;
import com.xijia.wuyouwallet.utils.MyLogger;
import com.xijia.wuyouwallet.utils.StringUtils;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/25 0025.
 */

public class FillPhoneCodeActivity extends BaseActivity implements View.OnClickListener{

    private ImageView fillPhoneCode_back;
    private TextView fillPhoneCode_phone,fillPhoneCode_config,fillPhoneCode_getCode;
    private EditText fillPhoneCode_code_et;
    private int countSeconds = 60;// 倒数秒数
    private String phone,bankName,bankNum,bankType;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fill_phone_code);
        initView();
    }
    private Handler hanlder = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    if (countSeconds > 0) {
                        --countSeconds;
                        fillPhoneCode_getCode.setText(countSeconds + "s");
                        hanlder.sendEmptyMessageDelayed(0, 1000);
                    } else {
                        countSeconds = 60;
                        fillPhoneCode_getCode.setText("重新获取");
                        fillPhoneCode_getCode.setClickable(true);
                    }
                    break;
            }
        }
    };
    // 开始倒计时
    private void startCountBack() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                fillPhoneCode_getCode.setText(countSeconds + "s");
                hanlder.sendEmptyMessage(0);
                fillPhoneCode_getCode.setClickable(false);
            }
        });
    }
    private void initView(){
        fillPhoneCode_back = (ImageView) findViewById(R.id.fillPhoneCode_back);
        fillPhoneCode_back.setOnClickListener(this);
        fillPhoneCode_phone = (TextView) findViewById(R.id.fillPhoneCode_phone);
        fillPhoneCode_config = (TextView) findViewById(R.id.fillPhoneCode_config);
        fillPhoneCode_config.setOnClickListener(this);
        fillPhoneCode_getCode = (TextView) findViewById(R.id.fillPhoneCode_getCode);
        fillPhoneCode_getCode.setOnClickListener(this);
        fillPhoneCode_code_et = (EditText) findViewById(R.id.fillPhoneCode_code_et);

        phone = getIntent().getStringExtra("phone");
        bankName = getIntent().getStringExtra("bankName");
        bankNum = getIntent().getStringExtra("bankNum");
        bankType = getIntent().getStringExtra("bankType");
        fillPhoneCode_phone.setText(StringUtils.hintPhone(phone));
        getCode();
    }
    private void getCode(){
        Map<String,Object> map = new HashMap<>();
        map.put("phone",phone);
        showProgressDialog(true);
        XUtil.Post(UrlConfig.GETCODE_URL,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                dismissProgressDialog();
                //MyLogger.wLog().e("获取验证码result="+result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.getInteger("code")==200){
                    startCountBack();
                }
                showToast(jsonObject.getString("msg"));
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                dismissProgressDialog();
                MyLogger.wLog().e("获取验证码失败");
            }
        });
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fillPhoneCode_back:
                finish();
                break;
            case R.id.fillPhoneCode_getCode:
                getCode();
                break;
            case R.id.fillPhoneCode_config:
                checkCode();
                break;
        }
    }

    private void checkCode(){
        String code = fillPhoneCode_code_et.getText().toString().trim();
        if (code.isEmpty()){
            showToast("请输入验证码");
            return;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("phone",phone);
        map.put("code",code);
        showProgressDialog(true);
        XUtil.Post(UrlConfig.CHECKING_PHONECODE_URL,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                dismissProgressDialog();
                //MyLogger.wLog().e("验证短信code="+result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.getInteger("code")==200){
                    next();
                }else {
                    dismissProgressDialog();
                    showToast(jsonObject.getString("msg"));
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                dismissProgressDialog();
                //MyLogger.wLog().e("验证短信onError="+ex.toString());
            }
        });
    }

    private void next(){
        Map<String,Object> map = new HashMap<>();
        map.put("token", MyApp.userData.getData().getToken());
        map.put("user",MyApp.userData.getData().getUser_id());
        map.put("card_number",bankNum);
        map.put("card_type",bankType);
        map.put("card_tel",phone);
        map.put("card_bank",bankName);
        XUtil.Post(UrlConfig.BIND_BANKCARD_URL,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                dismissProgressDialog();
                //MyLogger.wLog().e("绑定银行卡result="+result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.getInteger("code")==200){
                    Bundle bundle = new Bundle();
                    bundle.putString("bankNum",bankNum);
                    bundle.putString("bankType",bankType);
                    bundle.putString("phone",phone);
                    //bundle.putString("","");
                    jumpToPage(AddcardSuccessActivity.class,bundle,true);
                }else {
                    showToast(jsonObject.getString("msg"));
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                dismissProgressDialog();
                MyLogger.wLog().e("绑定银行卡onError="+ex.toString());
            }
        });
    }

}
